<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:msxml="urn:schemas-microsoft-com:xslt"
  xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:MemberExtension="urn:MemberExtension" 
  exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets MemberExtension ">


<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:param name="currentPage"/>
<xsl:variable name="isMenu" select="/macro/isMenu"/>
<xsl:variable name="isBreadcrumb" select="/macro/isBreadcrumb"/>
<xsl:variable name="isSitemap" select="/macro/isSitemap"/>
<xsl:variable name="staticMenu" select="/macro/staticMenu"/>
<xsl:variable name="includeHomeNode" select="/macro/includeHomeNode"/>
<xsl:variable name="rootNode" select="/macro/rootNode"/>
<xsl:variable name="macroStartLevel" select="/macro/startLevel"/>
<xsl:variable name="macroEndLevel" select="/macro/endLevel"/>
<xsl:variable name="breadCrumbDivider" select="/macro/breadCrumbDivider"/>
<xsl:variable name="customClassName" select="/macro/customClassName"/>
<xsl:variable name="homeNode" select="$currentPage/ancestor-or-self::*[@level=1]"/>
<xsl:variable name="startLevel"><xsl:choose><xsl:when test="$macroStartLevel!=''"><xsl:value-of select="$macroStartLevel"/></xsl:when><xsl:otherwise>1</xsl:otherwise></xsl:choose></xsl:variable>
<xsl:variable name="endLevel"><xsl:choose><xsl:when test="$macroEndLevel!=''"><xsl:value-of select="$macroEndLevel"/></xsl:when><xsl:otherwise>99</xsl:otherwise></xsl:choose></xsl:variable>

<xsl:template match="/">
    <xsl:choose>
        <xsl:when test="$isMenu=1">
            <xsl:call-template name="MenuTemplate"></xsl:call-template>
        </xsl:when>
        <xsl:when test="$isBreadcrumb=1">
            <xsl:call-template name="BreadCrumbTemplate"></xsl:call-template>
        </xsl:when>
        <xsl:when test="$isSitemap=1">
          <xsl:call-template name="SitemapTemplate"></xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <span style="color:red">Please select navigation type</span>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<!-- Menu template -->
<xsl:template name="MenuTemplate">
  <ul class="{$customClassName}">
      <xsl:if test="$includeHomeNode=1">
          <xsl:call-template name="SingleItem">
              <xsl:with-param name="itm" select="$homeNode"/>
            </xsl:call-template>
        </xsl:if>
        <xsl:choose>
            <xsl:when test="$staticMenu=1 and $rootNode!='' and $homeNode/@id!=$rootNode">
                <xsl:for-each select="$currentPage/ancestor-or-self::*[@level=1]//*[@id=$rootNode]/*[@isDoc and string(umbracoNaviHide)!='1' and @level&lt;=$endLevel]">
                    <xsl:call-template name="MenuItem"></xsl:call-template>
                </xsl:for-each>
            </xsl:when>
            <xsl:when test="$startLevel!=''">
               <xsl:choose>
                <xsl:when test="$startLevel=1">
                      <xsl:for-each select="$currentPage/ancestor-or-self::*[@level=$startLevel]/*[@isDoc and string(umbracoNaviHide)!='1' and @level&lt;=$endLevel]">
                            <xsl:call-template name="MenuItem"></xsl:call-template>
                        </xsl:for-each>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:for-each select="$currentPage/ancestor-or-self::*[@level=number($startLevel)-1]/*[@isDoc and string(umbracoNaviHide)!='1' and @level&lt;=$endLevel]">
                            <xsl:call-template name="MenuItem"></xsl:call-template>
                        </xsl:for-each>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="$homeNode/*[@isDoc and string(umbracoNaviHide)!='1' and @level&lt;=$endLevel]">
                    <xsl:call-template name="MenuItem"></xsl:call-template>
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>
    </ul>
</xsl:template>

<!-- Menu Item Template -->
<xsl:template name="MenuItem">
  <li>
      <xsl:attribute name="class">level<xsl:value-of select="@level"/><xsl:if test="$currentPage/@id = current()/@id"> active</xsl:if><xsl:if test="$currentPage/ancestor-or-self::*/@id = @id"> on</xsl:if></xsl:attribute>
        <xsl:attribute name="id">id-<xsl:value-of select="@id"/></xsl:attribute>
        <a href="{umbraco.library:NiceUrl(@id)}">
            <xsl:attribute name="class">level<xsl:value-of select="@level"/><xsl:if test="$currentPage/@id = current()/@id"> active</xsl:if><xsl:if test="$currentPage/ancestor-or-self::*/@id = @id"> on</xsl:if></xsl:attribute>
            <xsl:choose>
                <xsl:when test="umbracoUrlName != ''">
                  <xsl:value-of select="umbracoUrlName" />
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="@nodeName" />
                </xsl:otherwise>
            </xsl:choose>
        </a>
        <xsl:if test="count(./*[@isDoc and string(umbracoNaviHide)!='1']) and @level!=$endLevel and $currentPage/ancestor-or-self::*/@id = @id">
            <ul>
                <xsl:for-each select="./*[@isDoc and string(umbracoNaviHide)!='1']">
                    <xsl:sort select="@sortOrder" order="ascending"/>
                    <xsl:call-template name="MenuItem"></xsl:call-template>
                </xsl:for-each>
            </ul>
        </xsl:if>
    </li>
</xsl:template>

<!-- Bread Crumb template -->
<xsl:template name="BreadCrumbTemplate">
  <ul class="{$customClassName}">
      <xsl:if test="$includeHomeNode=1">
          <xsl:call-template name="SingleItem">
              <xsl:with-param name="itm" select="$homeNode"/>
            </xsl:call-template>
        </xsl:if>
        <xsl:for-each select="$homeNode/*[@isDoc and @level&lt;=$endLevel and $currentPage/ancestor-or-self::*/@id = @id]">
            <xsl:call-template name="breadCrumbItem">
              <xsl:with-param name="position" select="position()"/>
            </xsl:call-template>
        </xsl:for-each>
    </ul>
</xsl:template>

<!-- Bread Crumb Item Template -->
<xsl:template name="breadCrumbItem">
<xsl:param name="position"/>

  <xsl:if test="string(umbracoBreadcrumbHide)!='1'">

        <xsl:if test="($includeHomeNode=1 or $position=1) and $breadCrumbDivider!=''">
            <li><xsl:value-of select="$breadCrumbDivider"/></li>
        </xsl:if>
        
        <li>
            <xsl:attribute name="class">level<xsl:value-of select="@level"/><xsl:if test="$currentPage/@id = current()/@id"> active</xsl:if><xsl:if test="$currentPage/ancestor-or-self::*/@id = @id"> on</xsl:if></xsl:attribute>
            <xsl:attribute name="id">id-<xsl:value-of select="@id"/></xsl:attribute>
            <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:attribute name="class">level<xsl:value-of select="@level"/><xsl:if test="$currentPage/@id = current()/@id"> active</xsl:if><xsl:if test="$currentPage/ancestor-or-self::*/@id = @id"> on</xsl:if></xsl:attribute>
                <xsl:choose>
                    <xsl:when test="umbracoUrlName != ''">
                      <xsl:value-of select="umbracoUrlName" />
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="@nodeName" />
                    </xsl:otherwise>
                </xsl:choose>
            </a>
        </li>
    </xsl:if>
    
    <xsl:for-each select="./*[@isDoc and $currentPage/ancestor-or-self::*/@id = @id]">
        <xsl:sort select="@sortOrder" order="ascending"/>
        <xsl:call-template name="breadCrumbItem"></xsl:call-template>
    </xsl:for-each>
</xsl:template>

<!-- Sitemap template -->
<xsl:template name="SitemapTemplate">
  <ul class="{$customClassName}">
      <xsl:if test="$includeHomeNode=1">
          <xsl:call-template name="SingleItem">
              <xsl:with-param name="itm" select="$homeNode"/>
            </xsl:call-template>
        </xsl:if>
        <xsl:choose>
            <xsl:when test="$rootNode!='' and $homeNode/@id!=$rootNode">
                <xsl:for-each select="$currentPage/ancestor-or-self::*[@level=1]//*[@id=$rootNode]/*[@isDoc and string(umbracoSitemapHide)!='1' and @level&lt;=$endLevel]">
                    <xsl:call-template name="SitemapItem"></xsl:call-template>
                </xsl:for-each>
            </xsl:when>
            <xsl:when test="$startLevel!=''">
               <xsl:choose>
                <xsl:when test="$startLevel=1">
                      <xsl:for-each select="$currentPage/ancestor-or-self::*[@level=$startLevel]/*[@isDoc and string(umbracoSitemapHide)!='1' and @level&lt;=$endLevel]">
                            <xsl:call-template name="SitemapItem"></xsl:call-template>
                        </xsl:for-each>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:for-each select="$currentPage/ancestor-or-self::*[@level=number($startLevel)-1]/*[@isDoc and string(umbracoSitemapHide)!='1' and @level&lt;=$endLevel]">
                            <xsl:call-template name="SitemapItem"></xsl:call-template>
                        </xsl:for-each>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="$homeNode/*[@isDoc and string(umbracoSitemapHide)!='1' and @level&lt;=$endLevel]">
                    <xsl:call-template name="SitemapItem"></xsl:call-template>
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>
    </ul>
</xsl:template>

<!-- Sitemap Item Template -->
<xsl:template name="SitemapItem">
  <li>
      <xsl:attribute name="class">level<xsl:value-of select="@level"/><xsl:if test="$currentPage/@id = current()/@id"> active</xsl:if><xsl:if test="$currentPage/ancestor-or-self::*/@id = @id"> on</xsl:if></xsl:attribute>
        <xsl:attribute name="id">id-<xsl:value-of select="@id"/></xsl:attribute>
        <a href="{umbraco.library:NiceUrl(@id)}">
            <xsl:attribute name="class">level<xsl:value-of select="@level"/><xsl:if test="$currentPage/@id = current()/@id"> active</xsl:if><xsl:if test="$currentPage/ancestor-or-self::*/@id = @id"> on</xsl:if></xsl:attribute>
            <xsl:choose>
                <xsl:when test="umbracoUrlName != ''">
                  <xsl:value-of select="umbracoUrlName" />
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="@nodeName" />
                </xsl:otherwise>
            </xsl:choose>
        </a>
        <xsl:if test="count(./*[@isDoc and string(umbracoSitemapHide)!='1']) and @level!=$endLevel">
            <ul>
                <xsl:for-each select="./*[@isDoc and string(umbracoSitemapHide)!='1']">
                    <xsl:sort select="@sortOrder" order="ascending"/>
                    <xsl:call-template name="SitemapItem"></xsl:call-template>
                </xsl:for-each>
            </ul>
        </xsl:if>
    </li>
</xsl:template>

<!-- Single Item Template -->
<xsl:template name="SingleItem">
<xsl:param name="itm"/>
  <li>
      <xsl:attribute name="class">level<xsl:value-of select="$itm/@level"/><xsl:if test="$currentPage/@id = $itm/@id"> active</xsl:if></xsl:attribute>
        <xsl:attribute name="id">id-<xsl:value-of select="$itm/@id"/></xsl:attribute>
        <a href="{umbraco.library:NiceUrl($itm/@id)}">
            <xsl:attribute name="class">level<xsl:value-of select="$itm/@level"/><xsl:if test="$currentPage/@id = $itm/@id"> active</xsl:if></xsl:attribute>
            <xsl:choose>
                <xsl:when test="$itm/umbracoUrlName != ''">
                  <xsl:value-of select="$itm/umbracoUrlName" />
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="$itm/@nodeName" />
                </xsl:otherwise>
            </xsl:choose>
        </a>
    </li>
</xsl:template>

</xsl:stylesheet>